package com.sgb.kjwl.view.ui.activity.self;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;


import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMyCarinfoBinding;
import com.sgb.kjwl.model.entity.MyCarInfoEntity;
import com.sgb.kjwl.model.entity.self.CarInfoEntity;
import com.sgb.kjwl.utils.EntityConversion;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.utils.VehicleConstant;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity;
import com.sgb.kjwl.view.ui.activity.self.logistics.AddCarActivity;
import com.sgb.kjwl.viewmodel.self.MyCarInfoModel;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.base.model.entity.RequestCode;
import com.swgk.core.dialog.ImagesPreviewDialog;
import com.swgk.core.util.DateUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 车主管理-车辆详情
 * by wangbinchen
 */
@Route(path = AppRouteConstant.WuLui.ROUTER_PATH_CAR_INFO)
public class MyCarInfoActivity extends AppActivity {
    MyCarInfoModel carInfoModel;
    ActivityMyCarinfoBinding binding;
    MyCarInfoEntity myCarInfo;
    private long carNo;
    List<String> url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_carinfo);
        carInfoModel = new ViewModelProvider(this).get(MyCarInfoModel.class);
        binding.include.titleView.setText("车辆审核详情");
        binding.include.rightView.setVisibility(View.GONE);
        binding.modifyInformation.setOnClickListener(this);
        binding.include.ivBack.setOnClickListener(this);
        binding.tvEdite.setOnClickListener(this);
        binding.addImg2.setOnClickListener(this::onClick);
        binding.addSecondImg.setOnClickListener(this::onClick);
        binding.addImg3.setOnClickListener(this::onClick);
        binding.addRoadImg1.setOnClickListener(this::onClick);
        binding.addRoadImg2.setOnClickListener(this::onClick);
        binding.addWorkImg2.setOnClickListener(this::onClick);
        binding.addWorkImg1.setOnClickListener(this::onClick);
        binding.carComplete.setOnClickListener(this::onClick);

    }

    @Override
    protected void init() {
        carNo = getIntent().getLongExtra("carNo", 0);
        if (carNo != 0) {
            carInfoModel.getJingDriverDetail(this,carNo);
            carInfoModel.driverDwtail.observe(this,list->{
                carDetail(list);
            });
        }
    }
//
//    @Override
//    protected void setupActivityComponent(AppComponent appComponent) {
//        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
//    }

    @Override
    protected void onResume() {
        super.onResume();
        carNo = getIntent().getLongExtra("carNo", 0);
        if (carNo != 0) {
            carInfoModel.getJingDriverDetail(this,carNo);
            carInfoModel.driverDwtail.observe(this,list->{
                carDetail(list);
            });
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.modify_information:
                if (myCarInfo == null) {
                    return;
                }
                AddCarActivity.start(getActivity(), Long.parseLong(myCarInfo.getNo()));
                break;
            case R.id.tv_edite:
                toModifyMore();
                break;
            case R.id.add_img_2:
                isEmpty(myCarInfo.getVehicleLicense());
                break;
            case R.id.add_second_img:
                //预览
                isEmpty(myCarInfo.getDrivingLicensePage());
                break;
            case R.id.add_img_3:
                isEmpty(myCarInfo.getCarImage());
                break;
            case R.id.add_road_img1:
                isEmpty(myCarInfo.getTransportCertificateMain());
                break;
            case R.id.add_road_img2:
                if (myCarInfo == null) {
                    return;
                }
                isEmpty(myCarInfo.getTransportCertificateVice());
                break;
            case R.id.add_work_img1://道路运输可能会为null不显示
                if (myCarInfo == null) {
                    return;
                }
                isEmpty(myCarInfo.getMainCertificate());

                break;
            case R.id.add_work_img2:
                if (myCarInfo == null) {
                    return;
                }
                isEmpty(myCarInfo.getViceCertificate());
                break;
            case R.id.car_complete:
                toModifyMore();
                break;
        }
    }

    public void carDetail(MyCarInfoEntity myCarInfo) {
        this.myCarInfo = myCarInfo;
        try {
        binding.setViewModel(myCarInfo);
        binding.carTypeInfo.setItemContent(myCarInfo.getCarTypeName());
        binding.carLenInfo.setItemContent(myCarInfo.getCarLenName());
        binding.carWeightInfo.setItemContent(NumberUtil.trim0(myCarInfo.getWeight()) + "吨");
        binding.llSerialNumber.setVisibility(TextUtils.isEmpty(myCarInfo.getTransportCertificateNum()) ? View.GONE : View.VISIBLE);
        if((myCarInfo.getDetailAddress().contains("省")&&myCarInfo.getDetailAddress().contains("市"))){
            binding.itemTvCarLocation.setText(myCarInfo.getDetailAddress());
        }else {
                binding.itemTvCarLocation.setText(String.format("%s%s%s%s",
                        myCarInfo.getProvinceName(),
                        myCarInfo.getCityName(),
                        myCarInfo.getDistrictName(),
                        myCarInfo.getDetailAddress()));
        }
        binding.carColorInfo.setItemContent(getLicensePlateColorWords(myCarInfo.getLicensePlateColor()));
        binding.driverTimeInfo.setItemContent(myCarInfo.getAcquireTime()==null||myCarInfo.getAcquireTime()==0?"/": DateUtils.timestampToDate(myCarInfo.getAcquireTime()));
        binding.carUpperDoorAddress.setItemContent(myCarInfo.getCarProvinceName()+myCarInfo.getCarCityName()+myCarInfo.getCarDistrictName());
        binding.carInsuranceTime.setItemContent(myCarInfo.getInsuranceTime()==null?"":DateUtils.timestampToDate(myCarInfo.getInsuranceTime()));
        binding.carAnnualInspectionTime.setItemContent(myCarInfo.getAnnualInspectionTime()==null?"":DateUtils.timestampToDate(myCarInfo.getAnnualInspectionTime()));
        binding.carMaintenanceTime.setItemContent(myCarInfo.getUpMaintainTime()==null?"":DateUtils.timestampToDate(myCarInfo.getUpMaintainTime()));
        binding.carIssuingAuthority.setItemContent(myCarInfo.getImageIssuingAuthority()==null?"":myCarInfo.getImageIssuingAuthority());
        binding.carVehicleEnergyType.setItemContent(myCarInfo.getImageVehicleEnergyType()==null?"":myCarInfo.getImageVehicleEnergyType());
        if (TextUtils.isEmpty(myCarInfo.getImageApprovedLoadingQuality())){
            binding.tvCarApprovedLoadMass.setVisibility(View.GONE);
        }else {
            binding.tvCarApprovedLoadMass.setVisibility(View.VISIBLE);
            binding.tvCarApprovedLoadMassText.setText(NumberUtil.trim0(myCarInfo.getImageApprovedLoadingQuality())+"kg");
        }
        if (TextUtils.isEmpty(myCarInfo.getImageSizeL())){
            binding.tvCarOverallDimension.setVisibility(View.GONE);
        }else {
            binding.tvCarOverallDimension.setVisibility(View.VISIBLE);
            binding.tvCarOverallDimensionText.setText(NumberUtil.trim0(NumberUtil.convertToDouble(myCarInfo.getImageSizeL()))+"mm");
        }if (TextUtils.isEmpty(myCarInfo.getImageVehicleAddress())){
            binding.tvCarAddress.setVisibility(View.GONE);
        }else {
            binding.tvCarAddress.setVisibility(View.VISIBLE);
            if((myCarInfo.getImageVehicleAddress().contains("省")&&myCarInfo.getImageVehicleAddress().contains("市"))){
                binding.tvCarAddressText.setText(myCarInfo.getImageVehicleAddress());
            }else {
                if (TextUtils.isEmpty(myCarInfo.getImageVehicleProvince())||myCarInfo.getImageVehicleAddress().contains(myCarInfo.getImageVehicleProvince())&&TextUtils.isEmpty(myCarInfo.getImageVehicleCity())||myCarInfo.getImageVehicleAddress().contains(myCarInfo.getImageVehicleCity())){
                    binding.tvCarAddressText.setText(myCarInfo.getImageVehicleAddress());
                }else {
                    binding.tvCarAddressText.setText(String.format("%s%s%s%s",
                            myCarInfo.getImageVehicleProvince(),
                            myCarInfo.getImageVehicleCity(),
                            myCarInfo.getImageVehicleDistrict(),
                            myCarInfo.getImageVehicleAddress()));
                }
            }
        }
        if (TextUtils.isEmpty(myCarInfo.getImageNumber())){
            binding.tvCarFileNumber.setVisibility(View.GONE);
        }else {
            binding.tvCarFileNumber.setVisibility(View.VISIBLE);
            binding.tvCarFileNumberText.setText(myCarInfo.getImageNumber());
        }
        if (TextUtils.isEmpty(myCarInfo.getIntentionProvinceName())&&TextUtils.isEmpty(myCarInfo.getIntentionCityName())){
            binding.currentDestination.setVisibility(View.GONE);
        }else {
            binding.currentDestination.setVisibility(View.VISIBLE);
        }
        binding.currentDestination.setItemContent(myCarInfo.getIntentionProvinceName()+" "+myCarInfo.getIntentionCityName());
       if (myCarInfo.getCarYears()!=null){
           if (myCarInfo.getCarYears()==0){
               binding.carVehicleLife.setVisibility(View.GONE);
               binding.carVehicleLife.setItemContent("");
           }else {
               binding.carVehicleLife.setVisibility(View.VISIBLE);
               binding.carVehicleLife.setItemContent(myCarInfo.getCarYears()==null?"":myCarInfo.getCarYears().toString()+"年");
           }
       }else {
           binding.carVehicleLife.setVisibility(View.GONE);
       }
       if (!TextUtils.isEmpty(myCarInfo.getLinkman())){
           binding.vehiclePrincipal.setText(myCarInfo.getLinkman());
       }else {
           binding.llVehiclePrincipal.setVisibility(View.GONE);
       }
       if (!TextUtils.isEmpty(myCarInfo.getLinkmanPhone())){
           binding.personInChargePhone.setText(myCarInfo.getLinkmanPhone());
       }else {
           binding.llPersonInChargePhone.setVisibility(View.GONE);
       }
        if (myCarInfo.getIsComplete()==0){
            binding.carInfoDetail.setVisibility(View.GONE);
            binding.carComplete.setVisibility(View.VISIBLE);
            binding.tvEdite.setVisibility(View.GONE);
        }else {
            binding.carInfoDetail.setVisibility(View.VISIBLE);
            binding.carComplete.setVisibility(View.GONE);
            binding.tvEdite.setVisibility(View.VISIBLE);
        }
        setCarInfoShow();
        if (myCarInfo.getStatus() == 1 && myCarInfo.getState() == 0) {
            binding.tvState.setText("已下架");
            binding.tvStatusDetails.setVisibility(View.VISIBLE);
            binding.tvStatusDetails.setText(myCarInfo.getRemark());
        } else {
            switch (myCarInfo.getStatus()) {
                case 0:
                    binding.tvState.setText("审核中");
                    binding.modifyInformation.setVisibility(View.GONE);
                    binding.llStatusDetails.setVisibility(View.VISIBLE);
                    binding.tvState.setTextColor(Color.parseColor("#5792FD"));
                    binding.tvStatusDetails.setText(myCarInfo.getRemark());
                    binding.tvStatusDetails.setTextColor(getResources().getColor(R.color.color_666666));
                    break;
                case 1:
                    binding.tvState.setText("审核通过");
                    binding.modifyInformation.setVisibility(View.VISIBLE);
                    binding.tvStatusDetails.setVisibility(View.VISIBLE);
                    binding.tvState.setTextColor(getResources().getColor(R.color.color_06C764));
                    binding.llStatusDetails.setVisibility(View.GONE);
                    break;
                case 2:
                    binding.tvState.setText("审核不通过");
                    binding.modifyInformation.setVisibility(View.VISIBLE);
                    binding.llStatusDetails.setVisibility(View.VISIBLE);
                    binding.tvStatusDetails.setText(myCarInfo.getRemark());
                    binding.tvStatusDetails.setTextColor(getResources().getColor(R.color.color_FFC000));
                    binding.tvState.setTextColor(Color.parseColor("#EA4237"));
                    break;
            }
        }
        bindMoreInfo();
        }catch (Exception e){

        }
    }

    private void bindMoreInfo() {
        binding.moreInfoContent.setVisibility(View.VISIBLE);
        if (TextUtils.isEmpty(myCarInfo.getHeightHurdle())) {
            binding.rlHeight.setVisibility(View.GONE);
        } else {
            binding.rlHeight.setVisibility(View.VISIBLE);
            binding.iitHeightValue.setText(NumberUtil.trim0(myCarInfo.getHeightHurdle()) + "米");
        }
        if (TextUtils.isEmpty(VehicleConstant.getIsDumpStr(myCarInfo.getIsDump()))) {
            binding.iitIsunloadValue.setVisibility(View.GONE);
        } else {
            binding.iitIsunloadValue.setVisibility(View.VISIBLE);
            binding.iitIsunloadValue.setItemContent(VehicleConstant.getIsDumpStr(myCarInfo.getIsDump()));
        }
        if (TextUtils.isEmpty(myCarInfo.getFunctionNos())) {
            binding.vehicleFunctionValue.setVisibility(View.GONE);
        } else {
            binding.vehicleFunctionValue.setVisibility(View.VISIBLE);
            binding.vehicleFunctionValue.setItemContent(myCarInfo.getFunctionNos());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RequestCode.REQUEST_EDITE_CAR_MORE && resultCode == RESULT_OK) {
            //刷新数据
            carInfoModel.getJingDriverDetail(this,carNo);
            carInfoModel.driverDwtail.observe(this,list->{
                carDetail(list);
            });
        }
    }

    private void toModifyMore() {
        if (myCarInfo == null) {
            return;
        }
        //todo 编辑更多信息
        //需要回调fragment返回值，所以用原生跳转
        Intent intent = new Intent(this, AddCarInfoActivity.class);
        intent.putExtra("carNo", Long.valueOf(myCarInfo.getNo()));
        intent.putExtra("carInfo", EntityConversion.modelA2B(myCarInfo, CarInfoEntity.class));
        startActivityForResult(intent, RequestCode.REQUEST_EDITE_CAR_MORE);
    }

    private void isEmpty(String uri) {
        if (myCarInfo == null) {
            return;
        }
        if (TextUtils.isEmpty(uri)) {
            return;
        }
        ImagesPreviewDialog.newInstance(uri).show(this);
    }

    public String getLicensePlateColorWords(String licensePlateColor) {
        if (TextUtils.isEmpty(licensePlateColor)) {
            licensePlateColor = "/";
        } else if ("1".equals(licensePlateColor)) {
            licensePlateColor = "蓝色";
        } else if ("2".equals(licensePlateColor)) {
            licensePlateColor = "黄色";
        } else if ("9".equals(licensePlateColor)) {
            licensePlateColor = "绿色";
        }
        return licensePlateColor;
    }
    private void setCarInfoShow(){
        url = new ArrayList<>();
        url.add(TextUtils.isEmpty(myCarInfo.getVehicleLicense())?"":myCarInfo.getVehicleLicense());
        url.add(TextUtils.isEmpty(myCarInfo.getDrivingLicensePage())?"":myCarInfo.getDrivingLicensePage());
        url.add(TextUtils.isEmpty(myCarInfo.getCarImage())?"":myCarInfo.getCarImage());
        if (myCarInfo.getIsComplete()==0)return;
        binding.rlRoad.setVisibility(View.VISIBLE);
        binding.roadLl.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(myCarInfo.getTransportCertificateMain())&&TextUtils.isEmpty(myCarInfo.getTransportCertificateVice())) {
            binding.addRoadImg1Text.setText("主页");
            url.add(myCarInfo.getTransportCertificateMain());
        } else if (!TextUtils.isEmpty(myCarInfo.getTransportCertificateVice())&&TextUtils.isEmpty(myCarInfo.getTransportCertificateMain())){
            binding.addRoadImg1Text.setText("副页");
            url.add(myCarInfo.getTransportCertificateVice());
        }else if (!TextUtils.isEmpty(myCarInfo.getTransportCertificateMain())&&!TextUtils.isEmpty(myCarInfo.getTransportCertificateVice())){
            binding.addRoadImg1Text.setText("主页");
            binding.addRoadImg2Text.setText("副页");
            url.add(myCarInfo.getTransportCertificateMain());
        }else {
            if (TextUtils.isEmpty(myCarInfo.getTransportCertificateNum())){
                binding.rlRoad.setVisibility(View.GONE);
            }else {
                binding.roadLl.setVisibility(View.GONE);
                binding.rlRoad.setVisibility(View.VISIBLE);
            }

        }
        binding.rlWorkPic.setVisibility(View.VISIBLE);
        if (TextUtils.isEmpty(myCarInfo.getMainCertificate())&&TextUtils.isEmpty(myCarInfo.getViceCertificate())){
            binding.rlWorkPic.setVisibility(View.GONE);
        }else {
            binding.addWorkImg1Text.setText("主页");
            binding.addWorkImg2Text.setText("副页");
            url.add(myCarInfo.getMainCertificate());
            url.add(myCarInfo.getViceCertificate());
        }
    }
}
